package com.yanqu.road.server.cmd.activity.hsisland;

import com.yanqu.road.entity.activity.hsisland.enums.HsIslandBattleFieldEnemyEnum;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.server.manager.activity.hsisland.*;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.HsIslandProto;
import com.yanqu.road.server.cmd.CrossUnionCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.hsisland.param.HsIslandBattleOpResult;
import com.yanqu.road.server.manager.activity.hsisland.pb.CrossHsIslandActivityPb;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C2_HS_ISLAND_BATTLE_FIELD_OPERATE, desc = "战场操作")
public class CrossHsIslandBattleFieldOperateCmd extends CrossUnionCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        HsIslandProto.HsIslandBattleFieldOperateReqMsg reqMsg = HsIslandProto.HsIslandBattleFieldOperateReqMsg.parseFrom(packet.getBytes());
        HsIslandProto.HsIslandBattleFieldOperateRespMsg.Builder respMsg = HsIslandProto.HsIslandBattleFieldOperateRespMsg.newBuilder();
        CrossHsIslandActivity activity = CrossHsIslandActivityMgr.getActivity(reqMsg.getActivityId(), groupId);
        if (activity == null) {
            respMsg.setRet(GameErrorCode.E_HS_ISLAND_ACTIVITY_NOT_FOUND);
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_HS_ISLAND_BATTLE_FIELD_OPERATE, respMsg));
            return;
        }
        try {
            respMsg.setRet(0);
            respMsg.setType(reqMsg.getType());
            if (reqMsg.getType() == 1) {
                // 攻击怪物
                HsIslandBattleOpResult result = activity.attackMonster(packet.getPlayerId(), reqMsg.getUnionUserIdList());
                //构造PB
                CrossHsIslandUser user = activity.getUser(packet.getPlayerId());
                respMsg.setBattleResult(CrossHsIslandActivityPb.buildHsIslandBattleResult(result.getBattleResult()));
                respMsg.setUserData(CrossHsIslandActivityPb.buildHsIslandUserData(user));
                //添加打理奖励
                if (reqMsg.getAutoFlag()) {
                    if (result.getReward() != null) {
                        user.addAutoReward(result.getReward().getReward());
                    }
                }
                // 战场信息
                if (result.getBattleField().getEnemyType() == HsIslandBattleFieldEnemyEnum.ENEMY_BOSS) {
                    // boss不给掉血详情
                    respMsg.setMonsterData(CrossHsIslandActivityPb.buildHsIslandMonsterData(activity.getActivityId(), activity.getGroupId(), result.getBattleField().getMonsterData(), false));
                } else {
                    respMsg.setMonsterData(CrossHsIslandActivityPb.buildHsIslandMonsterData(activity.getActivityId(), activity.getGroupId(), result.getBattleField().getMonsterData(), true));
                }
                // 我的个人信息
                respMsg.setMyUserData(CrossHsIslandActivityPb.buildHsIslandBattleFieldUserData(result.getBattleField(), user));
                if (result.getEventData() != null) {
                    respMsg.setEventData(CrossHsIslandActivityPb.buildHsIslandEventData(result.getEventData(), false));
                }
                if (result.getReward() != null) {
                    respMsg.setRewardData(CrossHsIslandActivityPb.buildHsIslandRewardData(result.getReward()));
                    //回包游戏服
                    MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_HS_ISLAND_BATTLE_FIELD_OPERATE_FROM_CROSS, respMsg));
                } else {
                    //回包客户端
                    MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_HS_ISLAND_BATTLE_FIELD_OPERATE, respMsg));
                }
            } else if (reqMsg.getType() == 2) {
                // 攻击玩家
                activity.attackPlayer(packet.getPlayerId(), reqMsg.getTargetUserId());
            } else if (reqMsg.getType() == 3) {
                // 我的商会信息
                CrossHsIslandUser user = activity.getUser(packet.getPlayerId());
                CrossHsIslandBattleField battleField = activity.getBattleField(user.getUserData().getBattleFieldId());
                if (battleField != null) {
                    respMsg.setMyUnionData(CrossHsIslandActivityPb.buildHsIslandBattleFieldUnionData(battleField, user));
                    MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_HS_ISLAND_BATTLE_FIELD_OPERATE, respMsg));
                } else {
                    respMsg.setRet(GameErrorCode.E_HS_ISLAND_BATTLE_FIELD_NOT_FOUND);
                    MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_HS_ISLAND_BATTLE_FIELD_OPERATE, respMsg));
                }

            } else {
                respMsg.setRet(GameErrorCode.E_HS_ISLAND_BATTLE_FIELD_OPERATE_ERROR);
                MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_HS_ISLAND_BATTLE_FIELD_OPERATE, respMsg));
            }
        } catch (BusinessException e) {
            respMsg.setRet(e.getCode());
            MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.U_HS_ISLAND_BATTLE_FIELD_OPERATE, respMsg));
        }

    }

    @Override
    public void sendData() {}
}
